package com.sziit.se.highconcurrency.common.exception;

import com.sziit.se.highconcurrency.common.result.Result;
import com.sziit.se.highconcurrency.common.result.ResultUtil;
import com.sziit.se.highconcurrency.common.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 全局空指针异常处理
     *
     * @param request servlet请求体
     * @param e       异常
     * @return 消息体
     */
    @ExceptionHandler(NullPointerException.class)
    public Result nullPointExceptionHandler(final HttpServletRequest request, final NullPointerException e) {
        log.error("请求路径{}，发生空指针异常:{}", request.getRequestURI(), e);
        return ResultUtil.error(Constants.ResponseCode.SERVER_ERROR, "服务器异常");
    }

    /**
     * 全局数据库异常处理
     *
     * @param request servlet请求体
     * @param e       异常
     * @return 消息体
     */
    @ExceptionHandler(SQLException.class)
    public Result sqlExceptionHandler(final HttpServletRequest request, final SQLException e) {
        log.error("请求路径{}，操作数据库失败：{}", request.getRequestURI(), e);
        return ResultUtil.error("数据库操作失败");
    }

    @ExceptionHandler(MissingServletRequestPartException.class)
    public Result missingServletRequestPartExceptionHandler(final MissingServletRequestPartException e) {
        log.error("注入用户信息失败：{}", e);
        return ResultUtil.error("注入用户信息失败");
    }

    @ExceptionHandler(CustomException.class)
//    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public Result customExceptionHandler(final HttpServletRequest request, final CustomException e) {
        log.warn("请求路径{},抛出自定义异常:{}", request.getRequestURI(), e.getMessage());
        return ResultUtil.error(e.getCode(), e.getMessage());
    }

//    @ExceptionHandler(UnAuthenticationException.class)
//    @ResponseStatus(code = HttpStatus.UNAUTHORIZED)
//    public Result unAuthenticationExceptionHandler(final HttpServletRequest request, final CustomException e) {
//        log.warn("请求路径{},抛出自定义异常:{}", request.getRequestURI(), e.getMessage());
//        return ResultUtil.error(e.getCode(), e.getMessage());
//    }

    @ExceptionHandler(RuntimeException.class)
    public Result runtimeExceptionHandler(final HttpServletRequest request, final  RuntimeException e){
        log.warn("请求路径{}，发生运行时异常", e);
        return ResultUtil.error(e.getMessage());
    }

//    @ExceptionHandler(Exception.class)
//    public ResponseEntity<String> handleException(Exception e) {
//        // 这里可以记录日志，或者进行更复杂的异常处理
//        // 返回包含异常信息的响应实体
//        return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//    }

        @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        // 这里可以记录日志，或者进行更复杂的异常处理
        // 返回包含异常信息的响应实体
        return ResultUtil.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }
}
